package com.ruoyi.pv.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.doudou.core.container.ListUtil;
import com.doudou.core.container.MapUtil;
import com.doudou.core.date.HolidayUtil;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.pv.mapper.PvHolidayMapper;
import com.ruoyi.pv.domain.PvHoliday;
import com.ruoyi.pv.service.IPvHolidayService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;

/**
 * 节假日Service业务层处理
 *
 * @author zzx
 * @date 2023-07-11
 */
@Service
public class PvHolidayServiceImpl implements IPvHolidayService {
    private final static Log log = LogFactory.get(IPvHolidayService.class);

    @Autowired private PvHolidayMapper pvHolidayMapper;

    @Autowired private RedisCache redisCache;

    /**
     * 查询节假日
     *
     * @param id 节假日主键
     * @return 节假日
     */
    @Override
    public PvHoliday selectPvHolidayById(Integer id) {
        return pvHolidayMapper.selectPvHolidayById(id);
    }

    /**
     * 查询节假日列表
     *
     * @param pvHoliday 节假日
     * @return 节假日
     */
    @Override
    public List<PvHoliday> selectPvHolidayList(PvHoliday pvHoliday) {
        return pvHolidayMapper.selectPvHolidayList(pvHoliday);
    }

    /**
     * 新增节假日
     *
     * @param pvHoliday 节假日
     * @return 结果
     */
    @Override
    public int insertPvHoliday(PvHoliday pvHoliday) {
        pvHoliday.setCreateTime(DateUtils.getNowDate());
        int i = pvHolidayMapper.insertPvHoliday(pvHoliday);
        if ( i > 0){
            cacheHoliday(pvHoliday);;
        }
        return i;
    }

    /**
     * 修改节假日
     *
     * @param pvHoliday 节假日
     * @return 结果
     */
    @Override
    public int updatePvHoliday(PvHoliday pvHoliday) {
        int i = pvHolidayMapper.updatePvHoliday(pvHoliday);
        if (i>0){
            cacheHoliday(pvHoliday);;
        }
        return i;
    }

    /**
     * 批量删除节假日
     *
     * @param ids 需要删除的节假日主键
     * @return 结果
     */
    @Override
    public int deletePvHolidayByIds(Integer[] ids) {
        int i = pvHolidayMapper.deletePvHolidayByIds(ids);
        if (i>0){
            cacheHoliday();;
        }
        return i;
    }

    /**
     * 删除节假日信息
     *
     * @param id 节假日主键
     * @return 结果
     */
    @Override
    public int deletePvHolidayById(Integer id) {
        int i = pvHolidayMapper.deletePvHolidayById(id);
        if (i>0){
            cacheHoliday();;
        }
        return i;
    }

    @Override
    @Transactional
    public void updateThisHoliday() {
        //删除所有的记录
        pvHolidayMapper.deletePvHolidayAll();
        //获取、插入
        List<PvHoliday> holidays = HolidayUtil.getAllDayOff().entrySet().stream()
                .map(entry -> PvHoliday.builder().year(entry.getKey().year()).date(entry.getKey())
                        .dateStr(entry.getKey().toDateStr()).info(entry.getValue()).build())
                .collect(Collectors.toList());
        holidays.parallelStream().forEach(this::insertPvHoliday);
        //cacheHoliday();
    }

    /**
     * 判断是否是假日
     */
    @Override
    public boolean isHoliday(DateTime date) {
        return redisCache.getCacheMapValue(CacheConstants.PV_HOLIDAY_KEY + DateUtil.thisYear(),date.toDateStr()) != null;
    }

    @Override
    public boolean isNotHoliday(DateTime date) {
        return !isHoliday(date);
    }


    @Override
    public Map<String, PvHoliday> getAllHoliday() {
        Map<String, PvHoliday> holidayMap;
        if (ListUtil.isNotEmpty(holidayMap = redisCache.getCacheMap(CacheConstants.PV_HOLIDAY_KEY + DateUtil.thisYear()))){
            return holidayMap;
        }else {
            cacheHoliday();
            return getAllHoliday();
        }
    }

    /**
     * 将假日存入缓存
     */
    @PostConstruct
    public void cacheHoliday(){
        log.info("\n==========开始缓存节假日==========");
        List<PvHoliday> pvHolidays = pvHolidayMapper.selectPvHolidayList(new PvHoliday());
        Map<String, PvHoliday> map = MapUtil.map(true);
        for (PvHoliday holiday : pvHolidays) {
            map.put(holiday.getDateStr(),holiday);
        }
        redisCache.setCacheMap(CacheConstants.PV_HOLIDAY_KEY + DateUtil.thisYear(),map);
        log.info("\n==========缓存节假日成功:{}==========",map.size());
    }

    public void cacheHoliday(PvHoliday holiday){
        redisCache.setCacheMapValue(CacheConstants.PV_HOLIDAY_KEY + DateUtil.thisYear(),holiday.getDateStr(),holiday);
    }

}
